package na.learn.java.corepatterns.caching;

import na.learn.java.Logging.CacheLogger;
import na.learn.java.corepatterns.caching.factory.AbstractCacheFactory;
import na.learn.java.corepatterns.caching.factory.SimpleCacheFactory;

import org.apache.log4j.Logger;

public class CacheTest {

	Cacheable key;

	Cacheable value;

	AbstractCacheFactory cacheFactory;

	private static Logger logger;

	static {
		logger = CacheLogger.getLogger(LRUCache.class.getName());
	}

	public void testGetFromCache() {

		Cache testCache = getCache();

		Cacheable key, value, key15, key16 = /* :-)*/key15 = null;

		long startTime = System.currentTimeMillis();
		int count = 0;
		for (count = 0; count <= 10000; count++) {
			key = new MyKeyObject("" + count + 1);
			value = new MyValueObject("" + count + count);

			if (count == 15)
				key16 = key;
			else if (count == 14)
				key15 = key;

			testCache.add(key, value);
			//testCache.print();
			if (count == 20) {
				System.out.println("Got Value for key " + key16 + " ::::::" + testCache.get(key16));
				System.out.println("Got Value for key " + key16 + " ::::::" + testCache.get(key16));
				testCache.print();
			}

		}
		long endTime = System.currentTimeMillis();
		testCache.print();
		System.out.println("Duration for adding " + --count + " objects is:" + (endTime - startTime));
		System.out.println("An avg time for each addition: " + (((float) ((endTime - startTime)) / count)));

	}

	private Cache getCache() {
		Cleaner phasedLRUCleaner = new PhasedLRUCleaner();
		cacheFactory = new SimpleCacheFactory();

		return cacheFactory.getInstance("TestCache", 30, phasedLRUCleaner);
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		CacheTest tester = new CacheTest();

		tester.testGetFromCache();

	}

}
